package com.ruix.app.ui.stratum.setting;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.ruix.app.R;
import com.ruix.app.mapper.ClasssDetailMapper;
import com.ruix.app.model.ClassDetail;
import com.ruix.app.model.Classes;
import com.ruix.app.network.RXObserver;
import com.ruix.app.network.RequestFactory;
import com.ruix.app.ui.base.BaseActivity;

import io.reactivex.disposables.Disposable;

/**
 * Created by hadexs on 2017/4/25.
 */

public class ClassInfoActivity extends BaseActivity {

    public static Intent newIntent(Context context, Classes classes,ClassDetail classDetail) {
        Intent intent = new Intent(context, ClassInfoActivity.class);
        Bundle bundle = new Bundle();
        bundle.putSerializable("classes", classes);
        bundle.putSerializable("classDetail", classDetail);
        intent.putExtras(bundle);
        return intent;
    }

    private Classes currentClasses;
    private ClassDetail classDetail;

    private TextView suitiableView,benifiteView,courseView,mentorIntroView,
            classServiceView,classNameView,organizationView;
    private SimpleDraweeView classBannerView;


    @Override
    protected int loadViewLayout() {
        return R.layout.activity_class_info;
    }

    @Override
    protected void initView() {



        suitiableView = (TextView) findViewById(R.id.tv_suitable);
        benifiteView = (TextView) findViewById(R.id.tv_benifite);
        courseView = (TextView) findViewById(R.id.tv_course);
        mentorIntroView = (TextView) findViewById(R.id.tv_mentor_intro);
        classServiceView = (TextView) findViewById(R.id.tv_class_service);
        classNameView = (TextView) findViewById(R.id.tv_class_name);
        organizationView = (TextView) findViewById(R.id.tv_organization_name);
        classBannerView = (SimpleDraweeView) findViewById(R.id.class_banner);

    }

    private void updateView(ClassDetail detail){
        suitiableView.setText(detail.getSuitable());
        benifiteView.setText(detail.getBenifite());
        courseView.setText(detail.getCourse());
        mentorIntroView.setText(detail.getMentorIntro());
        classServiceView.setText(detail.getClassService());
        classBannerView.setImageURI(Uri.parse(detail.getBanner()));
        classNameView.setText(detail.getName());
        organizationView.setText(detail.getOrganization());
    }

    @Override
    protected void initLocalData() {
        Intent intent = getIntent();
        if (intent != null){
            currentClasses =  (Classes)intent.getSerializableExtra("classes");
            classDetail =  (ClassDetail) intent.getSerializableExtra("classDetail");

        }
        if (classDetail !=null){
            updateView(classDetail);
        } else {
            loadClassDetail();
        }

        if (currentClasses != null && currentClasses.isTeacherFriendCircle()){
            setTitle("圈子详情");
        } else {
            setTitle("班级详情");
        }
    }

    @Override
    protected void initListener() {

    }


    private void loadClassDetail(){
        if (currentClasses == null){
            return;
        }
        showLoading();
        RequestFactory.classDetail(currentClasses.getID(), new ClasssDetailMapper(currentClasses.getID()), new RXObserver<ClassDetail>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(ClassDetail value) {
                updateView(value);
            }

            @Override
            public void onError(Throwable e) {
                dismissLoading();
                resolveException(e);
            }

            @Override
            public void onComplete() {
                dismissLoading();
            }
        });

    }
}
